package com.fnn.interceptor;

import com.alibaba.fastjson2.JSON;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Map;

public class Login implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler) throws Exception {
        //获取请求参数（json格式数据），获取请求体原始数据流
        BufferedReader reader = req.getReader();
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        String jsonData = sb.toString();
        //将json字符串转换成map
        Map<String, String> mm = JSON.parseObject(jsonData, Map.class);


//        String user_email = mm.get("user_email");
//        String user_password = mm.get("user_password");
//
//        System.out.println("拦截器" + mm.get("user_email"));
//
//        if (user_email==null || user_password==null || user_email.length()<6 || user_email.length()>100 || user_password.length()<2 || user_password.length()>100) {
//            System.out.println(user_email);
//            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid account or password length");
//            return false;
//        }
        // 如果所有检查都通过，则继续处理请求
        if (mm.equals(null)){
            System.out.println(mm);
            return false;
        }else {
            System.out.println("zh");
            return true;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}